Save device's state after restart HA

Hello, would you like suggest me how to keep devices’ state (ex: fan is on) after reboot HA.

Thanks

1 Like

Use the recorder component

It didn’t worked :(. This is my code.

recorder:
  purge_keep_days: 2
  purge_interval: 1
  exclude:
    domains:
      - script
      - weather
      - sensor
  include:
    entities:
      - switch.fan
      - switch.logitech
      - switch.tv_samsung
      - switch.stb

What kind of switches are you using?

It’s here. I put in configuration.yaml

switch:
  - platform: broadlink     
    host: 192.168.1.9
    mac: '78:0f:77:fd:8f:41'
    type: rm2    
    switches:
      logitech:
        friendly_name: "Logitech Z906"
        command_on: 'JgBIAAABKJIUERQ1FBEUERQRFBEUERMRFBEUERQRFBEUERM2FBEUNRQRFBEUERQRExEUERQRFDUUNhQ1FDUUNhQ1FDYTNhQREwANBQ=='
        command_off: 'JgBIAAABKJIUERQ1FBEUERQRFBEUERMRFBEUERQRFBEUERM2FBEUNRQRFBEUERQRExEUERQRFDUUNhQ1FDUUNhQ1FDYTNhQREwANBQ=='
      tv_samsung:        
        friendly_name: "Samsung TV"
        command_on: 'JgBGAJaTFDYVNxQ2FRIVERUSFREVEhU1FjYVNhUSFBIVEhQSFRISOBYSFBIVNhU2FRIVERU3FBEWNhU2FRIUEhU3FDYVEhUADQUAAA=='
        command_off: 'JgBGAJSWEzcUOBQ3FBIUERQTEhIUExI4EzkTNxUSExIUEhMSFBIUERQTExEVNxQ3FBITEhQ4EzcUOBM3FBITEhQ4EzcVEhMADQUAAA=='
        
      fan:
        friendly_name: "Fan"
        command_on: 'JgCcACcPKg8JLCcRJw8JLScPDioLKg0qDCoNAAEFKQ8mEAsqKRAmDwssJxALKwwrCysHMAsAAQYnDyoNDCsnECURDCsmEA0pDCsNKgssDAABBSYRJhALKykNKBAMKSgRCisNKgwqDSkOAAEEJhEnEAsrJxAnDwgvJxAMKQ0rCiwMKg0AAQUnECYQDConECcQCiwnEQsrDCoNKgwqDAANBQAAAAAAAAAAAAAAAA=='
        command_off: 'JgCcACcPKg8JLCcRJw8JLScPDioLKg0qDCoNAAEFKQ8mEAsqKRAmDwssJxALKwwrCysHMAsAAQYnDyoNDCsnECURDCsmEA0pDCsNKgssDAABBSYRJhALKykNKBAMKSgRCisNKgwqDSkOAAEEJhEnEAsrJxAnDwgvJxAMKQ0rCiwMKg0AAQUnECYQDConECcQCiwnEQsrDCoNKgwqDAANBQAAAAAAAAAAAAAAAA=='
      stb:
        friendly_name: "STB"
        command_on: 'JgBQAAABKZIVERQRFBEUERQ1FREUERQRFDUVNRU2FDYUERQ2FDYUNhQRFDYUERQ2FBEUERQRFBEUNhQRFDYUERQ2FDYUNhQ2FAAFLwABKUgUAA0FAAAAAAAAAAA='
        command_off: 'JgBQAAABKZIVERQRFBEUERQ1FREUERQRFDUVNRU2FDYUERQ2FDYUNhQRFDYUERQ2FBEUERQRFBEUNhQRFDYUERQ2FDYUNhQ2FAAFLwABKUgUAA0FAAAAAAAAAAA='

I think this is a ‘fire-and-forget’ setup.

The devices/switches only receive the command, they do not feed back the status change to the device that issued the command, so HA will not know what the status is or even if the command was executed properly on your device.

If you want to keep the status after an HA restart (that’s what I guess you mean with reboot) you’ll probably have to create an input_boolean for each device that you control and trigger the status change of these input_booleans at the same time you issue the command.

You’d then have to read the value of these input_booleans and you can then show it in your GUI.

The tricky part is that it looks like 3 out of your 4 switches don’t have distinct on and off commands, it the same command that toggles then on and off. This means that your switches and booleans will easily get out of sync - which then in turn is hard to fix.

2 Likes