Lay-Z-Spa Hot Tub wi-fi pump automation

My current YAML which is working. It’s all based on what Bruce has done but changed where needed to work with the clever spa.

Once i’ve got all the functions working i’ll get a full package of code together.

#####################################
sensor:
  - platform: rest
    name: hottub_status
    scan_interval: 30
    timeout: 20    
    resource: "https://api.gizwits.com/app/devdata/xxxxxxxxxxxx/latest"  
    # xxxxxxxxxxxx is the did from the bindings sensor
    device_class: timestamp
    headers:
      Content-Type: application/json
      X-Gizwits-Application-Id: 805cc6a3f41b48aeae471e2fcb6ebc73
    method: GET
    value_template: "{{ value_json.updated_at | timestamp_custom ('%Y-%m-%dT%H:%M:%S+00:00') }}"
    json_attributes_path: "$.attr"
    json_attributes:
      - Heater
      - 03
      - Undercooling
      - Current_temperature
      - Filter
      - Temperature_setup
      - Superheat
      - Bubble
      - Overtime_filter
      - Timing
      - Check
      - Time_filter

  - platform: template
    sensors:
      hottub_pump_temp:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        friendly_name: "HotTub Water Temperature"
        unit_of_measurement: '°C'
        value_template: "{{ state_attr('sensor.hottub_status', 'Current_temperature') | int }}"

      hottub_water_temp:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        friendly_name: "HotTub Water Temperature"
        unit_of_measurement: '°C'        
        value_template: "{{ states('input_number.Temperature_setup') | int }}"

      hottub_summary:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        friendly_name: "HotTub Status"
        value_template: "{% if is_state('binary_sensor.hottub_online','off') %}offline{% elif is_state('sensor.hottub_error','on') %}error{% elif is_state('input_boolean.hottub_scheduled','on') and is_state('switch.hottub_filter','off') %}scheduled{% elif is_state('switch.hottub_power','off') %}off{% elif state_attr('sensor.hottub_status','heat_temp_reach')==1 %}at temperature{% elif is_state('switch.hottub_heat','on') %}heating{% elif is_state('switch.hottub_filter','off') %}on{% elif is_state('switch.hottub_heat','off') %}filtering{% elif is_state('switch.hottub_bubbles','off') %}heating{% else %}bubbling{% endif %}"

  - platform: history_stats
    name: HotTub Heating This Week
    entity_id: sensor.hottub_summary
    state: "heating"
    type: time
    end: "{{ now().replace(hour=0, minute=0, second=0) }}"
    duration:
      days: 7

rest_command:
  hottub_command: 
    method: POST
    headers:
      content_type: "application/json"
      X-Gizwits-Application-Id: "805cc6a3f41b48aeae471e2fcb6ebc73"
      X-Gizwits-User-token: #####your user token#####
    url: "https://api.gizwits.com/app/control/ZUnVqKmSwvvgGkVtOFxS9v"    
    payload: "{{hottub_command}}"

binary_sensor:
  - platform: template
    sensors:
      hottub_online:
        friendly_name: "HotTub Online"
        value_template: "{{ (now() | as_timestamp - states('sensor.hottub_status') | as_timestamp ) < 2000.0 }}"
        
switch:
  - platform: template
    switches:
      hottub_heat:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        unique_id: Heater
        friendly_name: Heater      
        value_template: "{% if state_attr('sensor.hottub_status', 'Heater') == 1 %}on{% else %}off{% endif %}"
        turn_on:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"Heater": 1} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status
        turn_off:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"Heater": 0} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status
  
      hottub_filter:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        unique_id: hottub_filter
        friendly_name: Filter  
        value_template: "{% if state_attr('sensor.hottub_status', 'filter') == 1 %}on{% else %}off{% endif %}"
        turn_on:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"filter": 1} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status
        turn_off:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"filter": 0} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status

      hottub_bubbles:
        availability_template: "{{ states('binary_sensor.hottub_online') }}"
        unique_id: Bubbles
        friendly_name: Bubbles      
        value_template: "{% if state_attr('sensor.hottub_status', 'Bubble') == 1 %}on{% else %}off{% endif %}"
        turn_on:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"Bubble": 1} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status
        turn_off:
          - service: rest_command.hottub_command
            data_template: 
              hottub_command: >
                {"attrs": {"Bubble": 0} }   
          - delay: 00:00:05
          - service: homeassistant.update_entity
            entity_id: sensor.hottub_status

Ok…I’m stuck, and just can’t make things work. I’ve got the basic rest working, and am getting the following for hot tub status via the rest api.


So its working, and I can see changes in state when I use the buttons on the tub controllers.

Using the following in my sensor.yaml file (which works):

  • platform: rest
    name: hottub_status
    resource: https://usapi.gizwits.com/app/devdata/youdontneedtoknow/latest
    headers:
    Content-Type: application/json
    X-Gizwits-Application-Id: 98754e684ec045528b073876c34c7348
    method: GET
    value_template: “{% if value_json.attr.temp_set > 0%}online{% else %}offline{%endif%}”
    json_attributes_path: “$.attr”
    json_attributes:
    • system_err2
    • wave_appm_min
    • heat_timer_min
    • heat_power
    • earth
    • wave_timer_min
    • system_err6
    • system_err7
    • system_err4
    • system_err5
    • heat_temp_reach
    • system_err3
    • system_err1
    • system_err8
    • system_err9
    • filter_timer_min
    • heat_appm_min
    • power
    • temp_set_unit
    • filter_appm_min
    • temp_now
    • wave_power
    • locked
    • filter_power
    • temp_set

but when I attempt to use any template sensors, they all show unavailable.

Like this one:

binary_sensor:

  • platform: template
    sensors:
    hottub_online:
    friendly_name: “HotTub Online”
    value_template: “{{ (now() | as_timestamp - states(‘sensor.hottub_status’) | as_timestamp ) < 2000.0 }}”

None of them work from Bruce’s code, they all say unavailable. I must be missing something stupid, and am pulling my hair out. Am I placing some of the code in the wrong place (configuration vs sensor YAML files, I have tried both doesn’t seem to matter). Any help would be appreciated!

I’ve changed some of my code since then.

It’s expecting the hottub_status sensor to show a timestamp as it’s state:

sensor:
  - platform: rest
    name: hottub_status
    scan_interval: 30
    timeout: 20    
#    resource: https://euapi.gizwits.com/app/devdata/{my did}/latest
    resource: !secret bestway_status_url
    device_class: timestamp
    headers:
      Content-Type: application/json
      X-Gizwits-Application-Id: 98754e684ec045528b073876c34c7348
    method: GET
    value_template: "{{ value_json.updated_at | timestamp_custom ('%Y-%m-%dT%H:%M:%S+00:00') }}"
    json_attributes_path: "$.attr"
    json_attributes:
      - temp_now          # Temperature of Water in Pump
      - temp_set          # Target Temperature
      - temp_set_unit     # Temperature displayed in C or F
      - power             # Power - 1:On, 0:Off
      - filter_appm_min   # Start Filter in x minutes
      - filter_power      # Power - 1:On, 0:Off
      - filter_timer_min  # Filter for x minutes      
      - heat_appm_min     # Start Heat in x minutes
      - heat_power        # Heater - 1:On, 0:Off
      - heat_timer_min    # Heat for x minutes
      - heat_temp_reach   # Target Temperature Reached
      - wave_power        # Bubbles - 1:On, 0:Off
      - wave_appm_min     # Start Bubbles in x minutes
      - wave_timer_min    # Bubbles for x minutes
      - locked            # Pump Keypad Locked
      - earth             # Earth Fault
      - system_err1       # Error 1 - Sensor Error / Water Flow Error / Debris Buildup
      - system_err2       # Error 2 - Water Flow Error
      - system_err3       # Error 3 - Temperature too low / Thermostat broken
      - system_err4       # Error 4 - Temperature too high / Thermostat broken / Thermostat needs reset
      - system_err5       # Error 5 - Temperature too high / Thermostat broken / Thermostat needs reset
      - system_err6       # Error 6 - Electrical Fault (Current Variation)
      - system_err7       # Error 7 - Electrical Fault
      - system_err8       # Error 8 - Thermostat needs reset
      - system_err9       # Error 9 - Internal Fuse Failure
      
# https://support.bestwayaftersales.co.uk/faq/get-help-with-your-lay-z-spa-pump/

I put the code above in. That part works. But again, any template sensors are not working correctly. I’m taking small chunks to make it easier to debug.

I took your code of template sensors for HotTub Online and HotTub Error. You can see them in the background. Hottub Online shows Off regardless of status, and Hottub Error shows unavailable.

At this point I’m just using your code above, and then the code for HT Online and Error.

What am I missing? Bruce really appreciate the help.

I’d advise you to look at the latest code in my GitHub.
Rather than the code posted in this thread.

I have the exact same problem but with a cleverspa using the same API.

My binary_sensor.hottub_online shows as off and this is the same if I run a curl command outside HA

But my sensor.hottub_status shows

Heater: 0
Undercooling: 0
Current_temperature: 15
Filter: 1
Temperature_setup: 40
Superheat: 0
Bubble: 0
Overtime_filter: 1
Timing: 0
Check: 0
Time_filter: 10200
friendly_name: hottub_status
device_class: timestamp

And has a timestamp as the state

Could this be something to do with only able to register the tub on a single phone ?

If I run a curl command to switch on the bubbles for example

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'X-Gizwits-User-token: TOKEN' --header 'X-Gizwits-Application-Id: 805cc6a3f41b48aeae471e2fcb6ebc73' -d '{"attrs": {"bubble": 1} } ' 'https://api.gizwits.com/app/control/DID'

I get

{
    "error_message": "device offline!",
    "error_code": 9042,
    "detail_message": null
}

But my phone is working fine which is where I got the DID, Token etc from.

EDIT - it was the tub, realised the app opens but couldn’t send any commands, maybe tub got confused whilst settings everything up don’t know.

Switched the tub off and back on and now all working.

Thank you Bruce for all the initial work and thank you musicman for the cleverspa specific bits.

Really appreciated

I am using the latest code in your GitHub. In my Sensor.yaml, I have this:

  • platform: rest
    name: hottub_status
    scan_interval: 30
    timeout: 20
    resource: https://usapi.gizwits.com/app/devdata/XXXYYYZZZ/latest
    device_class: timestamp
    headers:
    Content-Type: application/json
    X-Gizwits-Application-Id: 98754e684ec045528b073876c34c7348
    method: GET
    value_template: “{{ value_json.updated_at | timestamp_custom (’%Y-%m-%dT%H:%M:%S+00:00’) }}”
    json_attributes_path: “$.attr”
    json_attributes:
    • temp_now # Temperature of Water in Pump
    • temp_set # Target Temperature
    • temp_set_unit # Temperature displayed in C or F
    • power # Power - 1:On, 0:Off
    • filter_appm_min # Start Filter in x minutes
    • filter_power # Power - 1:On, 0:Off
    • filter_timer_min # Filter for x minutes
    • heat_appm_min # Start Heat in x minutes
    • heat_power # Heater - 1:On, 0:Off
    • heat_timer_min # Heat for x minutes
    • heat_temp_reach # Target Temperature Reached
    • wave_power # Bubbles - 1:On, 0:Off
    • wave_appm_min # Start Bubbles in x minutes
    • wave_timer_min # Bubbles for x minutes
    • locked # Pump Keypad Locked
    • earth # Earth Fault
    • system_err1 # Error 1 - Sensor Error / Water Flow Error / Debris Buildup
    • system_err2 # Error 2 - Water Flow Error
    • system_err3 # Error 3 - Temperature too low / Thermostat broken
    • system_err4 # Error 4 - Temperature too high / Thermostat broken / Thermostat needs reset
    • system_err5 # Error 5 - Temperature too high / Thermostat broken / Thermostat needs reset
    • system_err6 # Error 6 - Electrical Fault (Current Variation)
    • system_err7 # Error 7 - Electrical Fault
    • system_err8 # Error 8 - Thermostat needs reset
    • system_err9 # Error 9 - Internal Fuse Failure

In Configuration.yaml, this code:

binary_sensor:

  • platform: template
    sensors:
    hottub_online:
    friendly_name: “HotTub Online”
    value_template: “{{ (now() | as_timestamp - states(‘sensor.hottub_status’) | as_timestamp ) < 2000.0 }}”

123

Notice hotttub_status shows 5 hours ago. But I can click on it, and it will show an update a few minutes back. All of the individual items show correct (temp now, temp set, etc…see my pic in my last post).

The binary sensor never reads correct (HotTub Online) never shows on. I am assuming this is preventing the others from working correctly.

To keep things simple, just focused on the two pieces of code: hottub_status and HotTub online.

What am I missing or not doing correctly? Its killing me that I can figure this out.

I had the same thing, switched hottub off and on and it all came alive.

I’ve restarted it several times, no change.

This is great. Thanks for the work this made my life so much asier and managed to create some Automations from this,

Anyone located in the US, whom has this working? Would love to compare configs. I’m so close, and still stuck.,

How far have you got? What is working? what is not working?
Which app is your pump linked to?
Bestway app?

The app is linked to the Bestway app.

The is in my sensory.yaml, and works:

  • platform: rest
    name: hottub_status
    scan_interval: 30
    timeout: 20
    resource: https://usapi.gizwits.com/app/devdata/XXXYYYZZZ/latest
    device_class: timestamp
    headers:
    Content-Type: application/json
    X-Gizwits-Application-Id: 98754e684ec045528b073876c34c7348
    method: GET
    value_template: “{{ value_json.updated_at | timestamp_custom (’%Y-%m-%dT%H:%M:%S+00:00’) }}”
    json_attributes_path: “$.attr”
    json_attributes:
    • temp_now # Temperature of Water in Pump
    • temp_set # Target Temperature
    • temp_set_unit # Temperature displayed in C or F
    • power # Power - 1:On, 0:Off
    • filter_appm_min # Start Filter in x minutes
    • filter_power # Power - 1:On, 0:Off
    • filter_timer_min # Filter for x minutes
    • heat_appm_min # Start Heat in x minutes
    • heat_power # Heater - 1:On, 0:Off
    • heat_timer_min # Heat for x minutes
    • heat_temp_reach # Target Temperature Reached
    • wave_power # Bubbles - 1:On, 0:Off
    • wave_appm_min # Start Bubbles in x minutes
    • wave_timer_min # Bubbles for x minutes
    • locked # Pump Keypad Locked
    • earth # Earth Fault
    • system_err1 # Error 1 - Sensor Error / Water Flow Error / Debris Buildup
    • system_err2 # Error 2 - Water Flow Error
    • system_err3 # Error 3 - Temperature too low / Thermostat broken
    • system_err4 # Error 4 - Temperature too high / Thermostat broken / Thermostat needs reset
    • system_err5 # Error 5 - Temperature too high / Thermostat broken / Thermostat needs reset
    • system_err6 # Error 6 - Electrical Fault (Current Variation)
    • system_err7 # Error 7 - Electrical Fault
    • system_err8 # Error 8 - Thermostat needs reset
    • system_err9 # Error 9 - Internal Fuse Failure

and I get this. Notice HotTub_status says 5 hours. It always says 5 hours (greyed out back window)

But when I click it, I get the following; and it is correctly reading the details from the HotTub. I confirmed this work by changing the status of the tub on the control panel, and it reflects here.

But anything post this refuses to work.
To keep things simple…I literally only have the above code and the earlier code for HotTub_login and HotTub_bindings.

The only other code I place was the one binary sensor…

binary_sensor:

  • platform: template
    sensors:
    hottub_online:
    friendly_name: “HotTub Online”
    value_template: “{{ (now() | as_timestamp - states(‘sensor.hottub_status’) | as_timestamp ) < 2000.0 }}”

No matter what I do…it only shows as it being Off. If I create a sensor for HotTub pump temperature, It comes back as unavailable.

In an earlier post, I have a view shows this detail. At this point I’m not even trying any switches or commands…Just displaying data.

Could this be a problem with time and date formatting? Hence why something is not working? In your above code, I notice a “T” was inserted between date and time.

I’m so close, but yet so far. I’m stumped.

it’s a timezone issue.
The +00:00 bit on the end of the time format works for me because I’m in the UK.

I’m guessing you need +05:00
Or possibly -05:00

Give that a go and see what happens.

And away we “go”. That was it! -05:00, and now we got something working. Now to work through the rest, and make it work. Appreciate the help! I’m sure I will have a few more questions along the way.

23

Be aware that I use c and you are using f.
(Temp units)

So the calculations in the scheduling code will need to be adjusted if you use them.

Hi. i copyied the whole bruces_homeassistant_config/packages/areas/garden/hottub-bestway at main · B-Hartley/bruces_homeassistant_config · GitHub
and got every sensor working but when i try the switches i get this in my log.

Logger: homeassistant.components.rest_command
Source: components/rest_command/init.py:136
Integration: RESTful Command (documentation, issues)
First occurred: 20:02:42 (2 occurrences)
Last logged: 20:02:51
Error. Url: https://euapi.gizwits.com/app/control/HyxxxXXxXxxxxX4. Status code 400. Payload: b’{“attrs”: {“power”: 0} }’
Error. Url: https://euapi.gizwits.com/app/control/HyxxxXXxXxxxxX4. Status code 400. Payload: b’{“attrs”: {“heat_power”: 1} }’

i dont understand why i get this payload:b’

Working! Superb :slight_smile:

Hello everybody,

I’m new to Hassio and I feel a little lost.

So far I have only added and configured add-ons via the store. Now I don’t know how to properly start with the code. Where do I have to install the first part? I think the further procedure is already described very well here, I just know how to start correctly :slight_smile:

Many Thanks

Hi Yagyu,

I guess you want to know where to put the bits of code to get you API token and DID? I have done this for a Lay Z Spa.

Paste the hottub_login code into your configuration.yaml.

As the code uses !secret for hiding passwords you also need to put some information in a file called secrets.yaml. This should be in the same folder (config) as your configuration.yaml.

Add the hottub_email_password line to secrets.yaml, but change the bits for your email address and password. It needs the ones you use with the official app.

If your email is [email protected] and your password is MY_PASSWORD it would look like this
hottub_email_password: email=name%40hotmail.com&password=MY_PASSWORD

Restart HA. Add an entity card (I think). From memory the default card offered had the bits in I needed. If not look for entities with hottub. in the name.

If it works this card will give you your API token and DID. You then need to add these to your secrets.yaml as below.
hottub_api_did: did=AAaAaAAAAAAaaaaAAA9A9b&api_token=a1a1a1a1a2a2a2a2a12a1a1a1a2a2a2a2

2 Likes