What you learn today
- Count devices
- Count ignores the name
- Find the weakest link
Three living rooms, a gas sensor called “Wohnen” in each. You want to know: how many sensors are running at all? Where is the air worst? That is what Count, Minimum and Maximum are for, with a few quirks.
Count counts devices
Count returns the number of devices, not a value. The chip still checks the LogicType: if a matching device cannot read it, you get IncorrectLogicType. Every device can read PrefabHash.
lb count SENSOR PrefabHash Count # all gas sensors on the network
Count ignores the name
In the game code Count counts all devices with the matching prefab hash. It does not look at the name, not even with lbn:
lbn count SENSOR HASH("Wohnen") PrefabHash Count # counts ALL gas sensors
If a sensor “Aussen” hangs on the same network, it counts too. For a count by name there is a trick: every device reports its own PrefabHash. The sum over all devices with this name, divided by the hash, is their number:
lbn total SENSOR HASH("Wohnen") PrefabHash Sum
div count total SENSOR # only the sensors "Wohnen"
Sum respects the name. Without a match you get 0 divided by the hash, so 0. That case is right too.
The weakest link
Minimum finds the smallest value, Maximum the largest. For the air this means: the room with the least oxygen decides on the alarm, not the average. An average of 21 %, 21 % and 15 % looks harmless at 19 %. The minimum shows the 15 %.
lbn o2 SENSOR HASH("Wohnen") RatioOxygen Minimum
Minimum returns 0 when no device matches, Maximum returns −infinity. An O₂ minimum of 0 triggers your alarm, which is even right here. With the maximum it goes wrong, and the next lesson deals with that.
Compare the counted number with the number you built. If a sensor is missing you notice at once, even if the others report good values.
Diagram
Exercise
Task
Three gas sensors are called “Wohnen”, a fourth one is called “Aussen” and hangs on the same network.
- LED “O2min”: the lowest oxygen ratio of the sensors “Wohnen” in percent
- LED “Sensoren”: the number of sensors “Wohnen” (the outside sensor does not count)
- flashing light “Luftalarm”: on below 19 % oxygen or if fewer than 3 sensors “Wohnen” are present
Hints
Hint 1
For the weakest link use Minimum instead of Average.
Hint 2
Count also counts “Aussen”. Use lbn … PrefabHash Sum and divide by SENSOR.
Hint 3
Alarm: slt for the oxygen, slt for the count, then or.
Quiz
4 questions. You pass from 60 % – the server does the grading.
Complete the day
- Passed the simulator exercise
- Passed the quiz with at least 60 %
Without an account progress is saved in this browser only.
Report a mistake in this lesson
Please sign in to report. Sign in