4  Pitfall 1: The Same Value Does Not Arrive

“Alexa, turn on the light” works the first time, but from the second time on there is no response at all —— this was the first bug in the original project.

NoteMake a prediction

You write {"sendSlot": 5} to desired, and the device processes it and writes 5 back to reported as well. If you write {"sendSlot": 5} to desired once more, will a delta reach the device?

4.1 Experiment

With the device running in terminal A and watch in B, send a plain number twice from terminal C.

python device/send.py slot-raw 1
python device/send.py slot-raw 1
desired updated: {"sendSlot": 1} → version 6
desired updated: {"sendSlot": 1} → version 8

Only the first one reaches the device (terminal A).

[  13.8s] delta received (push): {"sendSlot": 1}
[  13.8s]   >>> IR send #3: slot 1 "TV volume+"
[  13.8s]   <<< reported update: {"sendSlot": 1}

The second update is accepted and version increases, yet nothing happens. No error appears either.

4.2 Why it does not arrive

A delta was “the fields of desired whose value differs from reported” (rule 3 in Chapter 1).

desired reported delta
First send sendSlot: 1 (none) sendSlot: 1 → arrives
After the device writes back sendSlot: 1 sendSlot: 1 none
Second send sendSlot: 1 sendSlot: 1 none → does not arrive

The Shadow is “a mechanism for synchronizing state”. Even if asked to “be in the same state”, it does nothing because it is already in that state. But “pressing a button” is an event (a command), not a state. We want one send per press, but because the value is the same, it is treated as “no change”.

The same happens with the air conditioner.

python device/send.py ac-raw --temp 27
python device/send.py ac-raw --temp 27    # the second one does not arrive

“Set it to 27 degrees again” (you want to resend because the remote’s signal did not get through) does not arrive.

4.3 Countermeasure: mix in a value that changes every time

4.3.1 Command type (button): change the value itself every time

desired = {"sendSlot": f"{slot}-{int(time.time() * 1000)}"}   # "5-1791382552299"

Because the value changes on every send, it always becomes a delta. The device takes the part before - as the slot number and writes the received string back to reported as is.

4.3.2 State type (air conditioner): add a separate field, cmdId

Changing the air conditioner’s temp: 27 to "27-<time>" would break the meaning of the temperature. So we leave the state as it is and send a cmdId that changes every time as a separate field alongside it.

desired = {"temp": 27, "cmdId": str(int(time.time() * 1000))}

Even for the same temperature, cmdId changes, so the delta contains cmdId and arrives. Even when “only cmdId arrived”, the device treats it as an instruction to send the current state again.

python device/send.py ac --power on --temp 24
python device/send.py ac --power on --temp 24   # the second one arrives too

The second delta contains neither power nor temp, only cmdId (fields with the same value are not included in the delta).

[  26.6s] delta received (push): {"cmdId": "1791382573706"}
[  26.6s]   >>> IR send #5: air conditioner {'power': True, 'mode': 'cool', 'temp': 24, 'fan': 'auto'}
WarningThe separate-cmdId approach could not be used for buttons

In the original project we also tried {"sendSlot": 5, "cmdId": "..."} for buttons, but it did not work. If the value of sendSlot is the same as before, the delta does not contain sendSlot, only cmdId, so the device cannot tell “which button”. For commands like buttons, which convey “which one to execute” itself, use the approach of changing the value itself every time.

4.4 Check it in the simulator

Press “Light button (plain 5)” twice: the second time no delta appears and the light does not toggle. With “Light button (”5-timestamp”)” it toggles every time. Likewise for the air conditioner: “no cmdId” does not arrive the second time, while “with cmdId” arrives every time.

ImportantSummary of this chapter
  • The Shadow synchronizes state. If desired and reported have the same value, updating produces no delta (and no error)
  • For the command type (buttons), mix the time into the value itself so it changes every time
  • For the state type (air conditioner), leave the state as it is and add a cmdId that changes every time