Automation to set a state

Can anyone say, please, why this automation fails to work?

description: Set charge periods
mode: single
trigger:

  • platform: time
    at: “23:25:00”
    condition:
    action:
    service: foxess_modbus.update_all_charge_periods
    data:
    inverter: “********************************”
    charge_periods:
    - enable_force_charge: true
    enable_charge_from_grid: true
    start: “23:31”
    end: “23:59”
    - enable_force_charge: true
    - enable_charge_from_grid: true
    start: “00:01”
    end: “05:29”
    alias: Set charge periods

Thanks. The log gave me the clue on how to fix it.

However, now I’ve added a previously working automation in, I’m getting multiple duplicate and bad indentation errors on that automation which won’t rectify with the usual ’ -’ and pairs of spaces. Is there a way to cancel these errors?

description: Set charge periods
mode: single
trigger:

  • platform: time
    at: “19:02:00”
    condition:
  • condition: state
    entity_id: binary_sensor.fox_time_period_1_enable_charge_from_grid
    state: “off”
    action:
    - service: foxess_modbus.update_charge_period
    metadata: {}
    data:
    enable_force_charge: true
    enable_charge_from_grid: true
    start: “23:31:00”
    end: “23:59:00”
    charge_period: "1"
    inverter: 0fb69d3db5c0647101222c026b62XXXX
    - service: foxess_modbus.update_charge_period
    metadata: {}
    data:
    enable_force_charge: true
    enable_charge_from_grid: true
    start: “00:01:00”
    end: “05:29:00”
    inverter: 0fb69d3db5c0647101222c026b62XXXX
    charge_period: "2"
    alias: Set charge periods

description: “Stop battery discharging”
id: “Stop battery discharging”
mode: single
trigger:

  • platform: state
    entity_id:
    - sensor.myenergi_zappi_16090477_plug_status
    condition:
    action:
  • if:
    • condition: state
      entity_id: sensor.myenergi_zappi_16090477_plug_status
      state: “Charging”
      then:
    • device_id: 0fb69d3db5c0647101222c026b62XXXX
      domain: number
      entity_id: d48db2d924c3f48d60731452ea97XXXX
      type: set_value
      value: 0
      else:
    • condition: not
      conditions:
      • condition: state
        entity_id: sensor.myenergi_zappi_16090477_plug_status
        state: “Charging”
    • device_id: 0fb69d3db5c0647101222c026b62XXXX
      domain: number
      entity_id: d48db2d924c3f48d60731452ea97XXXX
      type: set_value
      value: 35
      alias: “Stop battery discharging”

there is no way for us to see the errors in syntax since you haven’t formatted the code snippet correctly.

put the code above between three backticks (```) on the line before and after the block of code.

Thanks, I had used backticks around lines with underscores (I’d seen that as a requirement ‘somewhere’) but here goes with just the pair.

description: "Set charge periods"
mode: single
trigger:
  - platform: time
    at: "23:25:00"
condition: []
action:
service: foxess_modbus.update_all_charge_periods
data:
inverter: "0fb69d3db5c0647101222c026b62XXXX"
charge_periods: 
    - enable_force_charge: true
      enable_charge_from_grid: true
      start: "23:31"
      end: "23:59"
enable_force_charge: true
enable_charge_from_grid: true
start: "00:01"
end: "05:29"
alias: "Set charge periods"

  - description: "Stop battery discharging"
id: "Stop battery discharging"
  - mode: single
  - trigger:
  - platform: state
    entity_id:
      - sensor.myenergi_zappi_1609XXXX_plug_status
  - condition: []
action:
  - if:
      - condition: state
        entity_id: sensor.myenergi_zappi_1609XXXX_plug_status
        state: "Charging"
    then:
      - device_id: 0fb69d3db5c0647101222c026b62XXXX
        domain: number
        entity_id: d48db2d924c3f48d60731452ea97XXXX
        type: set_value
        value: 0
    else:
      - condition: not
        conditions:
          - condition: state
            entity_id: sensor.myenergi_zappi_1609XXXX_plug_status
            state: "Charging"
      - device_id: 0fb69d3db5c0647101222c026b62XXXX
        domain: number
        entity_id: d48db2d924c3f48d60731452ea97XXXX
        type: set_value
        value: 35
  - alias: "Stop battery discharging"

Success on the repost!

Thanks. Each automation was created using the visual editor and both work if installed separately. The lower ‘stop battery discharging’ automation has had my attempts to fix the duplicate and indentation issues inflicted on it!

Is it possible to create the two of them in one single file using the visual editor? Once I had created the first I couldn’t see how to set up the trigger for the second.

Mangled because I had been trying to get it to work and posted it having forgotten what I had done. Apologies for that and for wasting your time!

I’ve posted the two automations below exactly as they were (separately) created in the Automation Editor. Both run successfully when installed separately but fail when installed together.

As you have seen, I’ve tried to remedy the duplication and indentation errors as they’re reported when I import them into /homeassistant/automations.yaml but whatever I try, especially with indentations, the errors seem to be insoluble.

Your continued help, please, would be invaluable.

description: "Stop battery discharging"
id: "Stop battery discharging"
mode: single
trigger:
  - platform: state
    entity_id:
      - sensor.myenergi_zappi_1609XXXX_plug_status
condition: []
action:
  - if:
      - condition: state
        entity_id: sensor.myenergi_zappi_1609XXXX_plug_status
        state: "Charging"
    then:
      - device_id: 0fb69d3db5c0647101222c026b62XXXX
        domain: number
        entity_id: d48db2d924c3f48d60731452ea97XXXX
        type: set_value
        value: 0
    else:
      - condition: not
        conditions:
          - condition: state
            entity_id: sensor.myenergi_zappi_1609XXXX_plug_status
            state: "Charging"
      - device_id: 0fb69d3db5c0647101222c026b62XXXX
        domain: number
        entity_id: d48db2d924c3f48d60731452ea97XXXX
        type: set_value
        value: 35
alias: "Stop battery discharging"

description: Set periods
mode: single
trigger:
  - type: not_powered
    platform: device
    device_id: 0fb69d3db5c0647101222c026b62XXXX
    entity_id: 7e2ac4461ff52513b6ad7c830ea0XXXX
    domain: binary_sensor
condition:
  - condition: time
    after: "23:25:00"
action:
  - service: foxess_modbus.update_charge_period
    data:
      enable_force_charge: true
      enable_charge_from_grid: true
      start: "23:31:00"
      end: "23:59:00"
      inverter: 0fb69d3db5c0647101222c026b62XXXX
      charge_period: "1"
  - service: foxess_modbus.update_charge_period
    data:
      enable_force_charge: true
      enable_charge_from_grid: true
      start: "00:01:00"
      end: "05:29:00"
      inverter: 0fb69d3db5c0647101222c026b62XXXX
      charge_period: "2"
alias: Set periods

I have to import them as I see ‘Response error 500’ every time I try to save them and I’m unable to discover what is causing the error.

You can’t paste a second automation into an existing automation. Nope. That’s why there are duplicate key errors.

Why don’t just just use the 3 dots on the automation that works, and choose duplicate then go in and edit the duplicate automation.

As you may have noticed this is all very new to me.! I get the Response error 500 every time I try to save an automation, even the very simplest. The ones I copied across after changing from Visual editor to Yaml worked immediately so I’m at a loss to know why they won’t save and why I get the error.

Both work if they’re copied into the Automations yaml separately. If they’re in there together, that’s when they fail. It seems it’s the Response error 500 that’s getting in the way. Are you aware of how to overcome it?

Well the first question to ask is why are you trying to copy them into automations.yaml ? They are automatically saved to automations.yaml when you create them in the editor and save them.

I have to copy them in because the Response error 500 doesn’t allow them to save from the editor.

I don’t know if it’s relevant but HA is running on a Synology NAS in a virtual machine.

In which case, tell it to save and get the 500 error, then go into the settings > system > logs and paste the logs that are shown so we can get an idea of what the problem is.

Done that now and found this:

2024-01-16T12:44:58.600Z [INFO] HA-WS heart beat result: "pong"
2024-01-16T12:46:49.348Z [INFO] HA emit call_service event: {"domain":"system_log","service":"write","service_data":{"logger":"frontend.js.latest.202401040","message":"Unhandled promise rejection from Chrome 120.0.0.0 on Windows 10\n{\n  \"error\": \"Response error: 500\",\n  \"status_code\": 500,\n  \"body\": \"500 Internal Server Error\\n\\nServer got itself in trouble\"\n}","level":"debug"}}
2024-01-16T12:46:58.606Z [INFO] HA-WS heart beat result: "pong"
2024-01-16T12:47:52.428Z [INFO] HA emit call_service event: {"domain":"system_log","service":"write","service_data":{"logger":"frontend.js.latest.202401040","message":"Unhandled promise rejection from Chrome 120.0.0.0 on Windows 10\n{\n  \"error\": \"Response error: 500\",\n  \"status_code\": 500,\n  \"body\": \"500 Internal Server Error\\n\\nServer got itself in trouble\"\n}","level":"debug"}}
2024-01-16T12:48:58.611Z [INFO] HA-WS heart beat result: "pong"
2024-01-16T12:50:58.617Z [INFO] HA-WS heart beat result: "pong"

I would try stopping Home Assistant, renaming automations.yaml to automations.yaml.bk and then starting Home Assistant again. it’s possible that it’s something already in automations.yaml that is causing the problem.

1 Like

Thanks, I’ll give it a go! (With HA stopped, how, where, would I find automations.yaml please?)

Yes, stop attempting to import anything into the automation.yaml and use the Automation Editor, in visual mode, to compose your automations from scratch.

I’m using the visual editor every time but every time it will not save and I see the 500 error.

I assumed that with HA stopped (how do I stop it to rename the file: I can only see how to shut it down?). I wouldn’t be able to see automations.yaml and would have to SSH into the virtual machine. I’m pretty new to this so am having to learn a lot at a fairly fast rate.